package org.skipper.samples.redission.hash;

import org.redisson.api.RFuture;
import org.redisson.api.RedissonClient;
import org.skipper.samples.redission.enums.RedisKeyEnum;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author shoujian
 * Date: 2021-08-12
 * Time: 下午7:38
 */
@Component
public class HashSource {

    @Qualifier(value = "redis-cloud_0")
    @Resource
    private RedissonClient redissonClient;

    public void hset() {
        //通过lua语言进行操作 local v = redis.call('hget', KEYS[1], ARGV[1]); redis.call('hset', KEYS[1], ARGV[1], ARGV[2]); return v
        redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).put("hset_put", System.currentTimeMillis());
        redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).putAsync("hset_putAsync", System.currentTimeMillis());
    }

    public void hsetnx() {
        redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).fastPutIfAbsent("hsetnx_fastPutIfAbsent", System.currentTimeMillis());
        redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).fastPutIfAbsentAsync("hsetnx_fastPutIfAbsentAsync", System.currentTimeMillis());
    }

    public void hget() {
        String hset_put = (String) redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).get("hset_put");
        RFuture<Object> hset_putAsync = redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).getAsync("hset_putAsync");
        System.out.println("hset_put = " + hset_put);
        System.out.println("hset_putAsync = " + hset_putAsync.getNow());
    }

    public void hexists() {
        boolean result = redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).containsKey("hexists_put");
        System.out.println("hexists_containsKey result = " + result);
        redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).containsKeyAsync("hset_putAsync");

    }

    public void hgetall() {
        Set<Object> hgetall_readAllKeySet = redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).readAllKeySet();
        System.out.println("hgetall_readAllKeySet = " + hgetall_readAllKeySet.toString());
    }

    public void hdel() {
        redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).fastRemove("hset_put");
        redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).fastRemoveAsync("hset_putAsync");
    }

    public void hstrlen() {
        int hstrlen_valueSize = redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).valueSize("hset_put");
        System.out.println("hstrlen_valueSize = " + hstrlen_valueSize);
    }

    public void hincrby() {
        Long hincrby_addAndGet = (Long) redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).addAndGet("hset_put", 10);
        System.out.println("hincrby_addAndGet = " + hincrby_addAndGet);
    }

    public void hkeys() {
        Set<Object> hkeys_readAllKeySet = redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).readAllKeySet();
        System.out.println("hkeys_readAllKeySet = " + hkeys_readAllKeySet.toString());
    }

    public void hlen() {
        int hlen_size = redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).size();
        System.out.println("hlen_size = " + hlen_size);
    }

    public void hmget() {
        Set<Object> data = new HashSet<>();
        data.add("hset_put");
        data.add("hset_putAsync");
        Map<Object, Object> hmget_getall = redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).getAll(data);
        System.out.println("hmget_getall = " + hmget_getall);
    }

    public void hmset() {
        Map<Object, Object> data = new HashMap<>();
        data.put("hset_put", System.currentTimeMillis());
        data.put("hset_putAsync", System.currentTimeMillis());

        redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).putAll(data);
    }

    public void hvals() {
        Collection<Object> hvals_readAllValues = redissonClient.getMap(RedisKeyEnum.HASH_TEST.getCode()).readAllValues();
        System.out.println("hvals_readAllValues = " + hvals_readAllValues);
    }



}
